package com.hzy.fivepalace.db.service;

import com.alibaba.fastjson2.JSONObject;
import com.hzy.fivepalace.db.dao.DtsSystemMapper;
import com.hzy.fivepalace.db.domain.DtsSystem;
import com.hzy.fivepalace.db.domain.DtsSystemExample;
import com.hzy.fivepalace.db.vo.ReturnAdressVo;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DtsSystemConfigService {
    @Resource
    private DtsSystemMapper systemMapper;

    public List<DtsSystem> queryAll() {
        DtsSystemExample example = new DtsSystemExample();
        example.or();
        return systemMapper.selectByExample(example);
    }

    public String getRandomAvatar() {
        String avatarUrl = null;
        DtsSystemExample example = new DtsSystemExample();
        example.orderBy("rand()");
        example.createCriteria().andKeyNameEqualTo("dts.user.avatar").andDeletedEqualTo(false).andKeyValueIsNotNull();
        DtsSystem dtsSystem = systemMapper.selectOneByExample(example);
        if (null != dtsSystem && !StringUtil.isBlank(dtsSystem.getKeyValue())) {
            avatarUrl = dtsSystem.getKeyValue();
        }
        return avatarUrl;
    }

    public int insertSelective(DtsSystem record) {
        return systemMapper.insertSelective(record);
    }

    public int updateByPrimaryKeySelective(DtsSystem record) {
        return systemMapper.updateByPrimaryKeySelective(record);
    }

    public DtsSystem selectByKey(String key) {
        DtsSystemExample example = new DtsSystemExample();
        example.createCriteria().andKeyNameEqualTo(key).andDeletedEqualTo(false);
        return systemMapper.selectOneByExample(example);
    }

    /**
     * 获取退款周期
     *
     * @return
     */
    public int getAfterSalePeriod() {
        int period = 7;//默认7天;
        DtsSystemExample example = new DtsSystemExample();
        example.createCriteria().andKeyNameEqualTo("dts.after.sale.period").andDeletedEqualTo(false);
        DtsSystem system = systemMapper.selectOneByExample(example);
        if (null != system && !StringUtil.isBlank(system.getKeyValue())) {
            try {
                period = Integer.parseInt(system.getKeyValue());
            } catch (Exception e) {
            }
        }
        return period;
    }

    /**
     * 获取退货地址
     * @return
     */
    public ReturnAdressVo getReturnAdress(){
        ReturnAdressVo vo=new ReturnAdressVo();
        DtsSystemExample example = new DtsSystemExample();
        example.createCriteria().andKeyNameEqualTo("dts.return.adress").andDeletedEqualTo(false);
        DtsSystem system = systemMapper.selectOneByExample(example);
        if (null != system && !StringUtil.isBlank(system.getKeyValue())) {
            try {
                vo= JSONObject.parseObject(system.getKeyValue(),ReturnAdressVo.class);
            } catch (Exception e) {
            }
        }

        return vo;
    }
}
